package com.example.treehole.ui.page.postDetail

import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.navigationBarsPadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ChatBubble
import androidx.compose.material.icons.filled.ChatBubbleOutline
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.material.icons.filled.FavoriteBorder
import androidx.compose.material.icons.filled.Star
import androidx.compose.material.icons.filled.StarBorder
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import com.example.treehole.LocalUserViewModel
import com.example.treehole.service.DataRepository
import com.example.treehole.ui.component.numberTransfer
import com.example.treehole.ui.model.CommentType
import com.example.treehole.ui.model.PostDetail
import com.example.treehole.ui.theme.backCyan
import com.example.treehole.ui.theme.iconBlue
import kotlinx.coroutines.launch

@Composable
fun BottomCommentBar(postDetail: PostDetail) {
    val userViewModel = LocalUserViewModel.current
    val userId by userViewModel.userId.collectAsState()
    val jwlToken by userViewModel.jwlToken.collectAsState()
    val scope = rememberCoroutineScope()

    val createCommentViewModel = LocalCreateCommentViewModel.current

    val isLike = remember { mutableStateOf(postDetail.isLike) }
    val isFavorite = remember { mutableStateOf(postDetail.isFavorite) }
    val likeCount = remember { mutableIntStateOf(postDetail.likeCount) }
    val favoriteCount = remember { mutableIntStateOf(postDetail.favoriteCount) }

    Row(
        modifier =
        Modifier
            .height(50.dp)
            .fillMaxWidth()
            .background(backCyan)
            .padding(start = 8.dp, top = 8.dp)
            .navigationBarsPadding(),
        verticalAlignment = Alignment.CenterVertically
    ) {
        Box(
            modifier =
            Modifier
                .background(Color.White, shape = RoundedCornerShape(16.dp))
                .border(1.dp, Color.Gray, RoundedCornerShape(16.dp))
                .height(56.dp)
                .weight(1f)
                .fillMaxWidth(),
            contentAlignment = Alignment.CenterStart
        ) {
            Text(
                text = "点击发送评论",
                style = MaterialTheme.typography.bodySmall,
                color = Color.Gray,
                modifier =
                Modifier
                    .padding(start = 8.dp)
                    .clickable {
                        createCommentViewModel.updateReplyUserName("")
                        createCommentViewModel.updateCommentingState(true)
                        createCommentViewModel.updateCmtCmtId(0)
                        createCommentViewModel.updateCmtType(CommentType.CommentToPost)
                    }
            )
        }
        Spacer(modifier = Modifier.width(16.dp))
        Row(
            modifier = Modifier
                .fillMaxWidth()
                .weight(2f),
            verticalAlignment = Alignment.CenterVertically,
            horizontalArrangement = Arrangement.SpaceEvenly
        ) {
            Row(
                verticalAlignment = Alignment.CenterVertically,
            ) {
                IconButton(
                    onClick = {
                        scope.launch {
                            userId?.let {
                                if (isLike.value && DataRepository.unlikePost(
                                        jwlToken,
                                        postDetail.postId,
                                        it
                                    ) ||
                                    (!isLike.value && DataRepository.likePost(
                                        jwlToken,
                                        postDetail.postId,
                                        it
                                    ))
                                ) {
                                    likeCount.intValue += if (isLike.value) -1 else 1
                                    isLike.value = !isLike.value
                                    postDetail.isLike = isLike.value
                                    postDetail.likeCount = likeCount.intValue
                                }
                            }
                        }
                    },
                    modifier = Modifier.size(24.dp)
                ) {
                    Icon(
                        imageVector =
                        if (isLike.value) Icons.Default.Favorite
                        else Icons.Default.FavoriteBorder,
                        contentDescription = "Likes",
                        tint = iconBlue,
                        modifier = Modifier.size(24.dp)
                    )
                }

                Spacer(modifier = Modifier.width(5.dp))
                Text(text = numberTransfer(likeCount.intValue), modifier = Modifier.width(48.dp))
            }
            Row(
                verticalAlignment = Alignment.CenterVertically,
            ) {
                Icon(
                    imageVector =
                    if (postDetail.isComment) Icons.Default.ChatBubble
                    else Icons.Default.ChatBubbleOutline,
                    contentDescription = "Comments",
                    tint = iconBlue
                )
                Spacer(modifier = Modifier.width(5.dp))
                Text(
                    text = numberTransfer(postDetail.commentCount),
                    modifier = Modifier.width(48.dp)
                )
            }
            Row(
                verticalAlignment = Alignment.CenterVertically,
            ) {
                IconButton(
                    onClick = {
                        scope.launch {
                            userId?.let {
                                if (isFavorite.value && DataRepository.unfavoritePost(
                                        jwlToken,
                                        postDetail.postId,
                                        it
                                    ) ||
                                    (!isFavorite.value && DataRepository.favoritePost(
                                        jwlToken,
                                        postDetail.postId,
                                        it
                                    ))
                                ) {
                                    favoriteCount.intValue += if (isFavorite.value) -1 else 1
                                    isFavorite.value = !isFavorite.value
                                    postDetail.isFavorite = isFavorite.value
                                    postDetail.favoriteCount = favoriteCount.intValue
                                }
                            }
                        }
                    },
                    modifier = Modifier.size(24.dp)
                ) {
                    Icon(
                        imageVector =
                        if (isFavorite.value) Icons.Default.Star
                        else Icons.Default.StarBorder,
                        contentDescription = "Favorites",
                        tint = iconBlue
                    )
                }
                Spacer(modifier = Modifier.width(5.dp))
                Text(
                    text = numberTransfer(favoriteCount.intValue),
                    modifier = Modifier.width(48.dp)
                )
            }
        }
    }
}
